<?php
/**
 * Created by Yevgen Vershynin.
 * User: Yevgen Vershynin
 * Description: Controller for Employee entity. Controls all operations for incoming requests.
 */
class EmployeeController
{
    private $employeeModel;

    public function __construct()
    {
        //Create EmployeeModel for further usage at request actions.
        $this->employeeModel = new EmployeeModel();
    }

    public function getAction($request)
    {
        //Check for available arguments in query and return Eployee by number or full list of Employees
        if(isset($request->urlElements[2])) {
            $officeId = (int)$request->urlElements[2];
            $data["message"] = "here is the info for Employee " . $officeId;
            $empl = $this->employeeModel->getEmployeeById($officeId);
            $data["office"] = $empl;
        } else {
            $data["message"] = "you want a list of Employees";
            $offices = $this->employeeModel->getAllEmployees();
            $data["offices"] = $offices;
        }
        return $data;
    }

    public function postAction($request)
    {
        $data = $request->parameters;
        //Update Employee with provided data.
        if(isset($request->urlElements[2]))
        {
            if($this->employeeModel->updateEmployee($data))
            {
                $data['message'] = "This data was submitted";
            }
            else
            {
                $data['message'] = "Data not saved. DB error.";
            }
        }
        else
        {
            $data['message'] = "Data not saved. Employee Number is not provided.";
        }

        return $data;
    }

    public function putAction($request)
    {
        $data = $request->parameters;
        //Create Employee with data from request
        if($this->employeeModel->createEmployee($data))
        {
            $data['message'] = "This data was submitted";
        }
        else
        {
            $data['message'] = "Data not saved.";
        }

        return $data;
    }

    public function deleteAction($request)
    {
        $data = $request->parameters;
        //Delete Employee by Employee number from DB.
        if(isset($request->urlElements[2]))
        {
            $emplId = (int)$request->urlElements[2];

            if($this->employeeModel->deleteEmployee($emplId))
            {
                $data['message'] = "This Employee was deleted";
            }
            else
            {
                $data['message'] = "This Employee was NOT deleted. DB error.";
            }
        }
        else
        {
            $data['message'] = "This Employee was NOT deleted. Office id is not provided";
        }

        return $data;
    }

}
